import java.awt.BorderLayout;
import java.awt.FlowLayout;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.filechooser.FileNameExtensionFilter;


public class SetTokenRewarded extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private IconResizer resizer = new IconResizer(null);


	/**
	 * Create the dialog.
	 */
	public SetTokenRewarded(final TokenBoard tb, final Problem prob) {
		setResizable(false);
		setBounds(100, 100, 147, 212);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel lblTokens = new JLabel("Token Rewarded:");
		lblTokens.setBounds(10, 107, 116, 14);
		contentPanel.add(lblTokens);
		
		final JComboBox comboBox = new JComboBox();
		
		for (int i = 0; i < tb.getNoOfTokens(); i++){
			Token token = tb.getToken(i);
			String filePath = token.getImage();
			String fileName = filePath.substring(12);
			comboBox.addItem(fileName.substring(0, fileName.length() - 4));
		}
		
		comboBox.setSelectedIndex(prob.getReinforcer());
		
		comboBox.setBounds(10, 123, 116, 22);
		contentPanel.add(comboBox);
		
		final JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setBounds(10, 11, 116, 92);
		lblNewLabel.setBorder(new LineBorder(Color.BLACK));
		contentPanel.add(lblNewLabel);
		
		if (comboBox.getItemCount() > 0){
			resizer.setImage(new ImageIcon("src/gallery/" + comboBox.getSelectedItem() + ".jpg"));
			resizer.resize(92, 116);
			lblNewLabel.setIcon(resizer.getImage());
		}
		
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				if (comboBox.getItemCount() > 0){
					resizer.setImage(new ImageIcon("src/gallery/" + comboBox.getSelectedItem() + ".jpg"));
					resizer.resize(92, 116);
					lblNewLabel.setIcon(resizer.getImage());
				} else {
					lblNewLabel.setIcon(null);
				}
			}
		});

		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						prob.setReinforcer(comboBox.getSelectedIndex());
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setVisible(true);
	}

}
